package com.shuidi.oilactivity.adapter;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.shuidi.oilactivity.R;
import com.shuidi.oilactivity.activity.AllCustomerActivity;
import com.shuidi.oilactivity.bean.Customer;

import java.util.List;

import cn.bmob.v3.b.V;

/**
 * Created by LuoZhihao on 2018/11/13.
 */
public class SmallCustomerAdapter extends RecyclerView.Adapter<SmallCustomerAdapter.ViewHolder> {

    private List<Customer> mList;
    private AlertDialog alertDialog;
    private Context mContext;

    static class ViewHolder extends RecyclerView.ViewHolder{
        TextView name;
        TextView iphone;
        TextView company;
        ImageButton call;
        View adapterView;
        ImageView img;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            adapterView = itemView;
            name = (TextView)itemView.findViewById(R.id.name);
            iphone = (TextView)itemView.findViewById(R.id.iphone);
            company = (TextView)itemView.findViewById(R.id.company);
            call = (ImageButton)itemView.findViewById(R.id.call);
            img = (ImageView)itemView.findViewById(R.id.img);
        }
    }

    public SmallCustomerAdapter(List<Customer> list){
        mList= list;
    }


    @NonNull
    @Override
    public SmallCustomerAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_small_customer,viewGroup,false);
        final ViewHolder holder = new ViewHolder(view);
        holder.call.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int position = holder.getAdapterPosition();
                Customer customer = mList.get(position);
                try {
                    Intent intent = new Intent(Intent.ACTION_CALL);
                    intent.setData(Uri.parse("tel:"+customer.getIphone()));
                    view.getContext().startActivity(intent);
                }catch (SecurityException e){
                    e.printStackTrace();
                }
            }
        });

        return holder;
    }



    @Override
    public void onBindViewHolder(@NonNull SmallCustomerAdapter.ViewHolder viewHolder, int i) {
        Customer customer = mList.get(i);
        viewHolder.name.setText(customer.getName());
        viewHolder.iphone.setText(customer.getIphone());
        if (customer.getUserType() == 1){
            viewHolder.img.setVisibility(View.GONE);
        }else {
            viewHolder.img.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }
}
